package com.wwwarehouse.contract.contract.modify;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.adapter.AddedSettingRulesGoodsAdapter;
import com.wwwarehouse.contract.adapter.SettingRulesGoodsAdapter;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.bean.ShoppingCartListBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.BindEventBus;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.RefreshEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 *         本次新增商品  商品设置规则
 */
@BindEventBus
public class AddedSettingRulesGoodsListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    private String pbUkid;

    private ArrayList<SettingRuleBean.DataBean.ListBean> tagBeanList = new ArrayList<>();
    AddedSettingRulesGoodsAdapter mAdapter;
    private int num;
    private int pos;
    private String sort;
    List<SettingRuleBean.DataBean.ListBean> mSelectList = new ArrayList<>();

    @Override
    protected int getContentId() {
        return R.layout.release_object_set_rule_list;
    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);

        if (getArguments() == null || getArguments().getSerializable("select_list") == null) {
            mLoadingView.showEmptyView(true);
            return;
        }
        List<SettingRuleBean.DataBean.ListBean> list = (List<SettingRuleBean.DataBean.ListBean>) getArguments().getSerializable("select_list");
        if (list == null || list.size() == 0) {
            mLoadingView.showEmptyView(true);
            mLoadingView.setEmptyListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestDatas();
                }
            });
            return;
        }
        pos = getArguments().getInt("pos") - 1;
        num = getArguments().getInt("num");
        mSelectList.clear();
        for (int i = pos * num; i < (pos + 1) * num; i++) {
            if (i >= 0 && i < list.size()) {
                mSelectList.add(list.get(i));
            }
        }
        if (mSelectList == null || mSelectList.size() == 0) {
            mLoadingView.showEmptyView(true);
            return;
        }
        initData();
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    private void initData() {
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
        final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                observer.removeGlobalOnLayoutListener(this);
                if (mAdapter == null) {
                    mAdapter = new AddedSettingRulesGoodsAdapter(mActivity, mSelectList, mRvContent.getHeight(), mRvContent.getWidth(), num, pos, pbUkid);
                }
                mRvContent.setAdapter(mAdapter);
            }
        });
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    public void onEventMainThread(Object event) {
        if (event instanceof RefreshEvent) {
            if (pos == ((RefreshEvent) event).getPage()) {
                tagBeanList.get(((RefreshEvent) event).getPosition()).setHasSetRule(true);
                tagBeanList.get(((RefreshEvent) event).getPosition()).setItemTempUkid(((RefreshEvent) event).getBean().getItemTempUkid());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPriceRuleType(((RefreshEvent) event).getBean().getPriceRuleType());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPriceRuleValue(((RefreshEvent) event).getBean().getPriceRuleValue());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setQtyRuleType(((RefreshEvent) event).getBean().getQtyRuleType());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setQtyRuleValue(((RefreshEvent) event).getBean().getQtyRuleValue());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPrice(((RefreshEvent) event).getBean().getPrice());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setQty(((RefreshEvent) event).getBean().getQty());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setUnit(((RefreshEvent) event).getBean().getUnit());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setDefinedUkid(((RefreshEvent) event).getBean().getDefinedUkid());
                tagBeanList.get(((RefreshEvent) event).getPosition()).setPbUkid(((RefreshEvent) event).getBean().getPbUkid());
                mAdapter.notifyItemChanged(((RefreshEvent) event).getPosition());
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mAdapter != null) {
            mAdapter.dissPop();
        }
    }
}
